<?php
/* [$WindsForce] (C)Hunzhiwange.COM Since 2012.03.17.
   家居公司控制器($$)*/

!defined('Q_PATH') && exit;

class JiajucompanyController extends AController{

	public function filter_(&$arrMap){
		$arrMap['A.jiajucompany_fullname']=array('like','%'.Q::G('jiajucompany_fullname').'%');
		$arrMap['A.jiajucompany_shortname']=array('like','%'.Q::G('jiajucompany_shortname').'%');

		$nJiajucompanyIsavip=Q::G('jiajucompany_isavip');
		$nJiajucompanyIsfvip=Q::G('jiajucompany_isfvip');
		if($nJiajucompanyIsavip!==null && $nJiajucompanyIsavip!=''){
			$arrMap['A.jiajucompany_isavip']=$nJiajucompanyIsavip;
		}
		if($nJiajucompanyIsfvip!==null && $nJiajucompanyIsfvip!=''){
			$arrMap['A.jiajucompany_isfvip']=$nJiajucompanyIsfvip;
		}

		// 时间设置
		$this->getTime_('A.create_dateline',$arrMap);
	}

	public function index($sModel=null,$bDisplay=true){
		parent::index('jiajucompany',false);
		$this->bAdd_();
		$this->display(Admin_Extend::template('jiaju','jiajucompany/index'));
	}

	protected function sqljoin_(){
		return "->joinLeft('".Q::C('DB_PREFIX')."site AS C','C.site_name','A.site_id=C.site_id')".
			"->joinLeft('".Q::C('DB_PREFIX')."district AS D','D.district_name AS province_name','A.province_id=D.district_id')".
			"->joinLeft('".Q::C('DB_PREFIX')."jiajucompanymaster AS F','F.jiajucompanymaster_lastlogintime','A.jiajucompany_id=F.jiajucompany_id')";
	}

	public function add(){
		$this->bAdd_();
		$this->display(Admin_Extend::template('jiaju','jiajucompany/add'));
	}
	
	public function bAdd_(){
		$oJiajucompanyclass=Q::instance('JiajucompanyclassModel');
		$this->assign('arrScale',$oJiajucompanyclass->getClass(1));
		$this->assign('arrOutput',$oJiajucompanyclass->getClass(4));
		$this->assign('arrHomeclass',$oJiajucompanyclass->getClass(2));
		$this->assign('arrBusinessclass',$oJiajucompanyclass->getClass(3));
		$this->assign('arrStyleclass',$oJiajucompanyclass->getClass(52));
	}

	public function edit($sMode=null,$nId=null,$bDidplay=true){
		$nId=intval(Q::G('value','G'));
		$this->bEdit_();
		parent::edit('jiajucompany',$nId,false);

		// 公司内容
		$sContent=JiajucompanycontentModel::F('jiajucompany_id=?',$nId)->getColumn('jiajucompany_content');
		$this->assign('sContent',$sContent);

		// 服务区域和类型
		$oJiajucompany=Q::instance('JiajucompanyModel');
		$this->assign('arrBusinessclass_',$oJiajucompany->getClass($nId,'jiajucompanybusinessclass'));
		$this->assign('arrDistrict_',$oJiajucompany->getClass($nId,'jiajucompanydistrict'));
		$this->assign('arrHomeclass_',$oJiajucompany->getClass($nId,'jiajucompanyhomeclass'));
		$this->assign('arrStyleclass_',$oJiajucompany->getClass($nId,'jiajucompanystyleclass'));

		$this->display(Admin_Extend::template('jiaju','jiajucompany/add'));
	}

	public function bEdit_(){
		$this->bAdd_();
	}

	function check_fullname(){
		$sJiajucompanyFullname=trim(Q::G('jiajucompany_fullname'));
		$nId=intval(Q::G('value'));

		if(!$sJiajucompanyFullname){
			exit('false');
		}

		// 查询条件
		$arrWhere=array();
		$arrWhere['jiajucompany_fullname']=$sJiajucompanyFullname;
		if($nId){
			$arrWhere['jiajucompany_id']=array('neq',$nId);
		}

		$oJiajucompany=JiajucompanyModel::F()->where($arrWhere)->setColumns('jiajucompany_id')->getOne();
		if(empty($oJiajucompany['jiajucompany_id'])){
			exit('true');
		}else{
			exit('false');
		}
	}

	public function insert($sModel=null,$nId=null){
		$oJiajucompany=new JiajucompanyModel();
		$oJiajucompany->saveData();
		if($oJiajucompany->isError()){
			$this->E($oJiajucompany->getErrorMessage());
		}

		$this->A($oJiajucompany->toArray(),Q::L('数据保存成功','Controller'),1);
	}

	public function update($sModel=null,$nId=null){
		$nId=Q::G('value');
		
		$oJiajucompany=JiajucompanyModel::F('jiajucompany_id=?',$nId)->getOne();
		$oJiajucompany->updateData();
		if($oJiajucompany->isError()){
			$this->E($oJiajucompany->getErrorMessage());
		}

		$this->S(Q::L('数据更新成功','Controller'));
	}

	public function master(){
		$nId=Q::G('value');

		if(!empty($nId)){
			$oJiajucompanymaster=JiajucompanymasterModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompanymaster['jiajucompany_id'])){
				$this->assign('oValue',$oJiajucompanymaster);
				
				// 保存信息
				if($this->isPost()){
					$oJiajucompanymaster->save('update');
					if($oJiajucompanymaster->isError()){
						$this->E($oJiajucompanymaster->getErrorMessage());
					}else{
						$this->S(Q::L('保存商家管理员信息成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/master'));
			}else{
				$this->E(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			$this->E(Q::L('操作项不存在','Controller'));
		}
	}

	public function verify(){
		$nId=Q::G('value');

		if(!empty($nId)){
			$oJiajucompanyverify=JiajucompanyverifyModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompanyverify['jiajucompany_id'])){
				$this->assign('oValue',$oJiajucompanyverify);

				// 读取当前的认证图片
				$arrVerifyImg=Model::F_('jiajucompanyverifyimg','jiajucompany_id=?',$nId)->getAll();
				$this->assign('arrVerifyImg',$arrVerifyImg);
				
				// 保存信息
				if($this->isPost()){
					$oJiajucompanyverify->save('update');
					if($oJiajucompanyverify->isError()){
						$this->E($oJiajucompanyverify->getErrorMessage());
					}else{
						// 清理工商认证信息
						Model::M_('jiajucompanyverifyimg')->deleteWhere(array('jiajucompany_id'=>$nId));
						
						if(!empty($_POST['jiajucompanyverifyimg'])){
							foreach($_POST['jiajucompanyverifyimg'] as $sValue){
								if($sValue){
									Model::M_('jiajucompanyverifyimg')->insertWhere(
										array(
											'jiajucompany_id'=>$nId,
											'jiajucompanyverifyimg_file'=>$sValue,
											'create_dateline'=>time()
										)
									);
								}
							}
						}
						
						$this->S(Q::L('保存商家工商注册信息成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/verify'));
			}else{
				$this->E(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			$this->E(Q::L('操作项不存在','Controller'));
		}
	}

	public function check_master(){
		$sJiajucompanymasterUsername=trim(Q::G('jiajucompanymaster_username'));
		$nId=intval(Q::G('value'));

		if(!$sJiajucompanymasterUsername){
			exit('true');
		}

		// 判断是否存在该帐号
		$oUser=UserModel::F('user_name=? AND user_status=1',$sJiajucompanymasterUsername)->setColumns('user_id')->getOne();
		if(empty($oUser['user_id'])){
			exit('false');
		}

		// 查询条件
		$arrWhere=array();
		$arrWhere['jiajucompanymaster_username']=$sJiajucompanymasterUsername;
		if($nId){
			$arrWhere['jiajucompany_id']=array('neq',$nId);
		}

		$oJiajucompanymaster=JiajucompanymasterModel::F()->where($arrWhere)->setColumns('jiajucompany_id')->getOne();
		if(empty($oJiajucompanymaster['jiajucompany_id'])){
			exit('true');
		}else{
			exit('false');
		}
	}

	public function foreverdelete_deep($sModel=null,$sId=null){
		$sId=Q::G('value');
		parent::foreverdelete_deep('jiajucompany',$sId);
	}

	public function foreverdelete($sModel=null,$sId=null,$bApp=false){
		$sId=Q::G('value');
		parent::foreverdelete('jiajucompany',$sId,true);
	}

	protected function aForeverdelete_deep($sId){
		$sId=Q::G('value','G');
		$arrIds=explode(',',$sId);
		
		// 解除活动相关数据
		if(is_array($arrIds)){
			foreach($arrIds as $nId){
				// 删除关联数据(评论 && 感兴趣和参加数据)
				$oEventcommentMeta=EventcommentModel::M();
				$oEventcommentMeta->deleteWhere(array('jiajucompany_id'=>$nId));
				if($oEventcommentMeta->isError()){
					$this->E($oEventcommentMeta->getErrorMessage());
				}

				$oEventuserMeta=EventuserModel::M();
				$oEventuserMeta->deleteWhere(array('jiajucompany_id'=>$nId));
				if($oEventuserMeta->isError()){
					$this->E($oEventuserMeta->getErrorMessage());
				}

				$oEventattentionuserMeta=EventattentionuserModel::M();
				$oEventattentionuserMeta->deleteWhere(array('jiajucompany_id'=>$nId));
				if($oEventattentionuserMeta->isError()){
					$this->E($oEventattentionuserMeta->getErrorMessage());
				}
			}
		}
	}

	public function forbid($sModel=null,$sId=null,$bApp=false){
		$nId=intval(Q::G('value','G'));
		parent::forbid('jiajucompany',$nId,true);
	}

	public function resume($sModel=null,$sId=null,$bApp=false){
		$nId=intval(Q::G('value','G'));
		parent::resume('jiajucompany',$nId,true);
	}

	public function clear_recycle($sModel=null,$sField='status'){
		parent::clear_recycle('jiajucompany',$sField);
	}

	public function vip(){
		$nId=intval(Q::G('value'));
		
		if(!empty($nId)){
			$oJiajucompany=JiajucompanyModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompany['jiajucompany_id']) || $oJiajucompany['jiajucompany_isfvip']==0){
				$oJiajucompanyvip=JiajucompanyvipModel::F('jiajucompany_id=?',$nId)->getOne();
				if(empty($oJiajucompanyvip['jiajucompany_id'])){
					exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
				}
				
				$this->assign('oValue',$oJiajucompany);
				$this->assign('oValueVip',$oJiajucompanyvip);

				// 保存信息
				if($this->isPost()){
					$oJiajucompanyvip->save('update');
					if($oJiajucompanyvip->isError()){
						$this->E($oJiajucompanyvip->getErrorMessage());
					}else{
						$this->S(Q::L('保存商家VIP设置成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/vip'));
			}else{
				exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			exit(Q::L('操作项不存在','Controller'));
		}
	}

	public function openvip(){
		$nId=intval(Q::G('value'));
		
		if(!empty($nId)){
			$oJiajucompany=JiajucompanyModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompany['jiajucompany_id'])){
				$oJiajucompanyvip=JiajucompanyvipModel::F('jiajucompany_id=?',$nId)->getOne();
				if(empty($oJiajucompanyvip['jiajucompany_id'])){
					exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
				}

				$this->assign('oValue',$oJiajucompany);
				$this->assign('oValueVip',$oJiajucompanyvip);

				// 保存信息
				if($this->isPost()){
					$sDomain=trim(Q::G('jiajucompany_domain'));
					
					// VIP服务检测
					if(Q::G('jiajucompany_isfvip')==1 && !$sDomain){
						$this->E('开通商铺VIP必须指定域名。');
					}

					// 检测域名是否重复
					if($sDomain){
						if(!Check::C($sDomain,'number_underline_english')){
							$this->E('商铺域名只能是英文、数字和下划线组成。');
						}
						
						$sCompanyDomain=Model::F_('jiajucompany','jiajucompany_id!=? AND jiajucompany_domain=?',$nId,$sDomain)->getColumn('jiajucompany_domain');
						if($sCompanyDomain){
							$this->E('商铺域名存在重复，请重新填写。');
						}
					}
					
					$oJiajucompany->save('update');
					if($oJiajucompany->isError()){
						$this->E($oJiajucompany->getErrorMessage());
					}else{
						// 保存VIP开通时间
						$oJiajucompanyvip->jiajucompanyvip_starttime=strtotime(Q::G('jiajucompanyvip_starttime'));
						$oJiajucompanyvip->jiajucompanyvip_endtime=strtotime(Q::G('jiajucompanyvip_endtime'));
						$oJiajucompanyvip->save('update');
						
						$this->S(Q::L('商家VIP服务设置成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/openvip'));
			}else{
				exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			exit(Q::L('操作项不存在','Controller'));
		}
	}

	public function cpverify(){
		$nId=intval(Q::G('value'));
		
		if(!empty($nId)){
			$oJiajucompany=JiajucompanyModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompany['jiajucompany_id'])){
				$this->assign('oValue',$oJiajucompany);

				// 保存信息
				if($this->isPost()){
					$oJiajucompany->jiajucompany_authsys=implode(',',Q::G('verify'));
					$oJiajucompany->save('update');
					if($oJiajucompany->isError()){
						$this->E($oJiajucompany->getErrorMessage());
					}else{
						$this->S(Q::L('商家认证服务设置成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/cpverify'));
			}else{
				exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			exit(Q::L('操作项不存在','Controller'));
		}
	}

	public function template(){
		$nId=intval(Q::G('value'));
		
		if(!empty($nId)){
			$oJiajucompany=JiajucompanyModel::F('jiajucompany_id=?',$nId)->getOne();
			if(!empty($oJiajucompany['jiajucompany_id']) || $oJiajucompany['jiajucompany_isfvip']==0){
				$oJiajucompanyvip=JiajucompanyvipModel::F('jiajucompany_id=?',$nId)->getOne();
				if(empty($oJiajucompanyvip['jiajucompany_id'])){
					exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
				}

				$arrTemplate=Jiaju_Extend::getThemePreview();

				// 当前模板
				$arrCurrentTemplates=Model::F_('jiajucompanybanner')->where(array('jiajucompany_id'=>$nId))->getAll();
				
				$this->assign('oValue',$oJiajucompany);
				$this->assign('oValueVip',$oJiajucompanyvip);
				$this->assign('arrTemplate',$arrTemplate);
				$this->assign('arrCurrentTemplates',$arrCurrentTemplates);

				// 保存信息
				if($this->isPost()){
					$oJiajucompanyvip->save('update');
					if($oJiajucompanyvip->isError()){
						$this->E($oJiajucompanyvip->getErrorMessage());
					}else{
						// 清理广告banner设置
						Model::M_('jiajucompanybanner')->deleteWhere(array('jiajucompany_id'=>$nId));

						if(!empty($_POST['jiajucompanybanner']['img'])){
							foreach($_POST['jiajucompanybanner']['img'] as $nKey=>$sValue){
								if($sValue){
									Model::M_('jiajucompanybanner')->insertWhere(
										array(
											'jiajucompany_id'=>$nId,
											'jiajucompanybanner_filename'=>$sValue,
											'jiajucompanybanner_filetitle'=>!empty($_POST['jiajucompanybanner']['title'][$nKey])?$_POST['jiajucompanybanner']['title'][$nKey]:'',
											'jiajucompanybanner_link'=>!empty($_POST['jiajucompanybanner']['link'][$nKey])?$_POST['jiajucompanybanner']['link'][$nKey]:'',
											'create_dateline'=>CURRENT_TIMESTAMP
										)
									);
								}
							}
						}
						
						$this->S(Q::L('保存商家VIP模板设置成功','__APPJIAJU_COMMON_LANG__@Controller'));
					}
				}
				
				$this->display(Admin_Extend::template('jiaju','jiajucompany/template'));
			}else{
				exit(Q::L('数据库中并不存在该项，或许它已经被删除','Controller'));
			}
		}else{
			exit(Q::L('操作项不存在','Controller'));
		}
	}
	
	public function view_preview($sMode=null,$nId=null,$bDidplay=true){
		$nId=intval(Q::G('value','G'));

		// 公司内容
		$sContent=JiajucompanycontentModel::F('jiajucompany_id=?',$nId)->getColumn('jiajucompany_content');
		$this->assign('sContent',$sContent);

		parent::view_preview('jiajucompany',$nId,false);
		$this->display(Admin_Extend::template('jiaju','jiajucompany/viewpreview'));
	}

	public function save_preview($sMode=null,$nId=null){
		$nId=intval(Q::G('value','G'));
		parent::save_preview('jiajucompany',$nId);
	}

	public function save_previewstatus($sMode=null,$nId=null){
		$nId=intval(Q::G('value','G'));
		parent::save_previewstatus('jiajucompany',$nId);
	}

}
